﻿using GeneralServices.Data;
using GeneralServices.ViewModel;
using GenericWin;
using Models;
using ReprisePV.ViewModels;
using ReprisePV.Views;
using ScreenConnection;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ReprisePV
{
    public class ReprisePVMainController : GenericMainController
    {
        private int val = 0;

        public ReprisePVMainController() : base(true) { }


        public override bool Authentificate(Dictionary<string, string> values)
        {
            return true;
        }

        public override Configuration BuildConfiguration()
        {
            Configuration config = new Configuration();
            config.Env = "DEV";
            config.InstanceBDD = "DSPP";
            config.LibelleEnvironnement = "Développent";
            config.LoginUserConnecte = "legouellecs";
            config.NomApplication = "Reprise PV";
            config.Version = "4.0.0";
            return config;
        }

        public override List<MenuItemViewModel> BuildMenu()
        {
            List<MenuItemViewModel> _list = new List<MenuItemViewModel>();

            MenuItemViewModel menuSession = new MenuItemViewModel("Session");
            menuSession.Children.Add(new MenuItemViewModel("ChangeUser", "Changer d'utilisateur", new Uri("pack://application:,,,/ReprisePV;component/Resources/Log-Off-28x28.png")));
            menuSession.Children.Add(new MenuItemViewModel("Help", "Aide", new Uri("pack://application:,,,/ReprisePV;component/Resources/cc-info3-28x28.png")));
            menuSession.Children.Add(new MenuItemViewModel("Close", "Quitter", new Uri("pack://application:,,,/ReprisePV;component/Resources/cc-stop-28x28.png")));

            MenuItemViewModel menuSearchPV = new MenuItemViewModel("Recherche PV");
            menuSearchPV.Children.Add(new MenuItemViewModel("SearchPV", "Rechercher un PV", new Uri("pack://application:,,,/ReprisePV;component/Resources/cc-viewmag-28x28.png")));

            MenuItemViewModel menuUser = new MenuItemViewModel("Utilisateurs");
            menuUser.Children.Add(new MenuItemViewModel("UserGestion", "Gestion Utilisateurs", new Uri("pack://application:,,,/ReprisePV;component/Resources/cc-edit-28x28.png")));


            _list.Add(menuSession);
            _list.Add(menuSearchPV);
            _list.Add(menuUser);

            return _list;
        }

        public override GenericUserControl DoAction(MenuItemViewModel item)
        {
            switch(item.Id)
            {
                case "ChangeUser":
                    Configuration config = GenericMainController.Config;
                    EcranConnection ecran = new EcranConnection(config.Version, config.NomApplication, config.Env, this, null);
                    ecran.Show();
                    base.LogOff();
                    return null;
                case "Close":
                    base.Quit();
                    return null;
                //case "Help":
                //    break;
                case "SearchPV":
                    return new SearchPV() { VM = ServicesSingle.Get<SearchPV_VM>() as SearchPV_VM };
                case "UserGestion":
                    return new GestionUtilisateur()
                     { VM = ServicesSingle.Get<GestionUtilisateur_VM>() as GestionUtilisateur_VM };
                default:
                    return null;
            }
        }

        public override void InitApplication()
        {

        }

        public override void LongLoadingApplication(Window w, int etape, int nbMaxEtape, String message)
        {
            Splashscreen s = w as Splashscreen;
            if (s != null)
                s.SetProgress(message, val++, nbMaxEtape);
        }

        public override void ShortLoadingApplication(Window w, int etape, int nbMaxEtape, String message)
        {
            Splashscreen s = w as Splashscreen;
            if (s != null)
                s.SetProgress(message, val++, nbMaxEtape);

            switch(etape)
            {
                case 1 :
                    StoreApplicationDbContext.Register<EntitiesReprisePV>("ContextPV", new EntitiesReprisePV());
                    break;
                case 2:
                    ServicesSingle.Register<DetailPV_VM>();
                    ServicesSingle.Register<GestionUtilisateur_VM>();
                    ServicesSingle.Register<SearchPV_VM>();                  
                    break;
                default:
                    break;
            }
        }
    }
}
